package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.po.Conversation;
import com.kxq.zhuoyue.service.ConversationService;
import com.kxq.zhuoyue.pageUtilEntity.Page;
import com.kxq.zhuoyue.util.UUID;
import com.kxq.zhuoyue.util.result.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-01
 */
@Slf4j
@RestController
@RequestMapping("/conversation")
public class ConversationController {

    @Autowired
    private ConversationService conversationService;

    /**
     * 分组展示
     * @param page
     * @return
     */
    @PostMapping("/getById")
    public AjaxResult getById(@RequestBody Page page){
        Map map = new HashMap<>();
        LambdaQueryWrapper<Conversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Conversation::getConversationGroupId,page.getConversationGroupId());
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<Conversation> list = conversationService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data",list);
        map.put("pageNum",pageInfo.getPageNum());
        map.put("pageSize",pageInfo.getPageSize());
        return AjaxResult.success(map);
    }

    /**
     * 添加
     * @param conversation
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Conversation conversation){
        String id = UUID.getOrderIdByTime();
        conversation.setConversationId(id);
        boolean save = conversationService.save(conversation);
        if (save){
            return AjaxResult.success("保存成功 ！");
        } else {
            return AjaxResult.success("保存失败 ！");
        }

    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Conversation conversation){
        boolean update = conversationService.updateById(conversation);
        if (update){
            return AjaxResult.success("修改成功 ！");
        } else {
            return AjaxResult.success("修改失败 ！");
        }
    }

    @DeleteMapping("/delete/{conversationId}")
    public AjaxResult delete(@PathVariable String conversationId){
        boolean b = conversationService.removeById(conversationId);
        if (b){
            return AjaxResult.success("删除成功 ！");
        } else {
            return AjaxResult.success("删除失败 ！");
        }
    }
}

